package com.edu.tankgame;

/**
 * 子弹的线程
 */
public class Shot implements Runnable {
    int x;//子弹的横坐标
    int y;//子弹的纵坐标
    int direct = 0;//子弹的方向
    int speed = 10;//子弹的速度
    boolean isLive = true;//子弹是否存活

    public Shot(int x, int y, int direct) {
        this.x = x;
        this.y = y;
        this.direct = direct;
    }
    //boolean
    @Override
    public void run() {
        while(true){
            //让子弹休眠
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            switch(direct){
                case 0://上
                    y -= speed;
                    break;
                case 1://右
                    x += speed;
                    break;
                case 2://下
                    y += speed;
                    break;
                case 3://左
                    x -= speed;
                    break;
            }
            //子弹碰到边界或打中敌方坦克
            if(!(x > 0 && x < 1000 && y > 0 && y < 750 && isLive)){
                isLive = false;
                break;
            }
        }
    }
}
